package com.samtech.web.config;



import java.util.Properties;

import javax.persistence.EntityManagerFactory;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.support.AbstractResourceBasedMessageSource;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.support.AbstractPlatformTransactionManager;

import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.Producer;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import com.samtech.commons.WebUtils;
import com.samtech.security.service.CaptchaBackground;

@Configuration
@ImportResource(locations= {"classpath*:spring/spring-datasource.xml","classpath*:spring/spring-context-shiro.xml"})
@ComponentScan(value= {"com.samtech.*.service","com.samtech.*.*.service"},excludeFilters= {@Filter(
		value={org.springframework.stereotype.Controller.class}
		)})
@EnableTransactionManagement(proxyTargetClass=true)
public class AppSpringConfig {
	
	
	@Bean
	@DependsOn(value="entityManagerFactory")
	AbstractPlatformTransactionManager transactionManager( EntityManagerFactory entityManagerFactory){
		/*DataSourceTransactionManager dsTransactionManager = new org.springframework.jdbc.datasource.DataSourceTransactionManager();
		dsTransactionManager.setDataSource(ds);
		return dsTransactionManager;*/
		//return new HibernateTransactionManager(sessionFactory);
		 JpaTransactionManager jpaTransactionManager = new org.springframework.orm.jpa.JpaTransactionManager();
		 jpaTransactionManager.setEntityManagerFactory(entityManagerFactory);
		 return jpaTransactionManager;
	}
	
	/**
	 * 
	 * @return
	 */
	/*@Bean(name="cacheManager")
	public CacheManager cacheManager(){
		EhCacheManagerFactoryBean cacheBean = new org.springframework.cache.ehcache.EhCacheManagerFactoryBean();
		cacheBean.setConfigLocation(new ClassPathResource("/cache/ehcache-local.xml"));
		cacheBean.afterPropertiesSet();
		return  cacheBean.getObject();
	}*/
	
	@Bean("captchaProducer")
	Producer captchaProducer(){
		DefaultKaptcha kaptchaProducer = new com.google.code.kaptcha.impl.DefaultKaptcha();
		Properties props=new Properties();
		props.setProperty(Constants.KAPTCHA_BORDER, "no");
		props.setProperty(Constants.KAPTCHA_IMAGE_WIDTH, "80");
		props.setProperty(Constants.KAPTCHA_IMAGE_HEIGHT, "34");
		props.setProperty(Constants.KAPTCHA_TEXTPRODUCER_CHAR_STRING, "ABCDEFGHKP2345689TVXY");
		props.setProperty(Constants.KAPTCHA_TEXTPRODUCER_CHAR_LENGTH, "4");
		props.setProperty(Constants.KAPTCHA_TEXTPRODUCER_CHAR_SPACE, "2");
		props.setProperty(Constants.KAPTCHA_TEXTPRODUCER_FONT_COLOR, "white");
		props.setProperty(Constants.KAPTCHA_BACKGROUND_IMPL, "com.samtech.security.service.CaptchaBackground");
		props.setProperty(CaptchaBackground.BACKGROUND_IMAGE_PATH_PROPERTY_NAME, "static/img/captcha");
		Config config=new Config(props);
		kaptchaProducer.setConfig(config);
		
		return kaptchaProducer;
	}
	
	@Bean(value="resourceMessageSource")
	AbstractResourceBasedMessageSource bldMessages(@Value("${message.basenames}")String basenames){//systemProperties.
		ReloadableResourceBundleMessageSource bundleMessageSource = new org.springframework.context.support.ReloadableResourceBundleMessageSource();
		bundleMessageSource.setCacheSeconds(120);
		bundleMessageSource.setUseCodeAsDefaultMessage(true);
		bundleMessageSource.setBasenames(basenames);
		return bundleMessageSource;
	}
	
	@Bean
	ApplicationListener<ContextRefreshedEvent> initEventBean() {
	 return	new ApplicationListener<ContextRefreshedEvent>() {
			@Override
			public void onApplicationEvent(ContextRefreshedEvent event) {
				WebUtils.setApplicationContext(event.getApplicationContext());
			}
			
		};
	}
}
